import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { Storage } from '@ionic/storage';
import { ServiceProvider } from '../../providers/service/service'
import { ApiProvider } from '../../providers/api/api'
import { Md5 } from 'ts-md5/dist/md5';
import { PublicCompoentsProvider } from '../../providers/public-compoents/public-compoents';

import { UserCashPage } from './../user-cash/user-cash';
import { UserRechargePage } from './../user-recharge/user-recharge';
import { UserBindBankPage } from './../user-bind-bank/user-bind-bank';
//2018-12-30
import { UserServicePage } from './../user-service/user-service';
import { UserCashRecordPage } from './../user-cash-record/user-cash-record';
import { UserHistoryPage } from './../user-history/user-history';
import { UserAgentPage } from './../user-agent/user-agent';





/**
 * Generated class for the MyPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
    selector: 'page-TabMy',
    templateUrl: 'tab-my.html',
})
export class TabMyPage {
    public list = [
        {
            icon: './assets/imgs/user/my1.png',
            title: '代理中心',
            url: 'UserAgentPage'
        }, {
            icon: './assets/imgs/user/my2.png',
            title: '购买详情',
            url: 'UserHistoryPage'
        }, {
            icon: './assets/imgs/user/my3.png',
            title: '提现记录',
            url: 'UserCashRecordPage'
        }, {
            icon: './assets/imgs/user/my4.png',
            title: '绑定银行卡',
            url: 'UserBindBankPage'
        }, {
            icon: './assets/imgs/user/my5.png',
            title: '在线客服',
            url: 'UserServicePage'
        }, {
            icon: './assets/imgs/user/my6.png',
            title: '清理缓存',
            url: ''
        }
    ];
    public resData: any = {};
    public userinfo: any = {};
    public uid: any;


    constructor(
        public navCtrl: NavController,
        public navParams: NavParams,
        public storage: Storage,
        public service: ServiceProvider,
        public Api: ApiProvider,
        public pubCom: PublicCompoentsProvider) {
        storage.get('userId').then(
            (value: string) => {
                this.uid = value;
                console.log(`constructor: ${this.uid}`)
            }
        );


    }

    ionViewDidEnter() {
        console.log('ionViewDidEnter MyPage');
        if (this.uid) {
            this.PostUserInfo();
        } else {
            this.PostWechatMes();
        }
    };

    // 获取个人信息
    PostWechatMes() {
        let timeStamp = (new Date()).getTime();
        this.Api.weChatMes({
            indexurl: window.location.href,
            // indexurl: 'http://second.registerme.cn/?openid=o9ow159Ui2ZLEHrh-gPZV6Mh7X5g&uid=2&is_tel=0&sign=663f337fb7a04c553eb8fb83a39cea02&user_token=4ddaad02d41a64c0ab50735b25b53f92',
            times: timeStamp,
            token: Md5.hashStr('HalfPriceMall' + timeStamp),
        }).then(res => {
            this.resData = res;
            if (this.resData.code == 1) {
                this.userinfo = this.resData.data;
                this.storage.set('userId', this.userinfo.id);
            }
        })
    }

    // 获取个人信息
    PostUserInfo() {
        let timeStamp = (new Date()).getTime();
        if (this.uid) {
            this.Api.userinfo({
                times: timeStamp,
                token: Md5.hashStr('HalfPriceMall' + timeStamp),
                userinfo: 'userinfo',
                userid: this.uid
            }).then(res => {
                console.log(res)
                this.resData = res;
                this.userinfo = this.resData.data;
            })
        }
    }

    enterChildPage(url) {
        if (url) {
            if (url == 'UserBindBankPage') {
                // 验证有没有绑定银行卡信息
                this.CheckBank();
            } else if (url == 'UserAgentPage') {
                this.navCtrl.push(UserAgentPage)
            } else if (url == 'UserHistoryPage') {
                this.navCtrl.push(UserHistoryPage)
            } else if (url == 'UserCashRecordPage') {
                this.navCtrl.push(UserCashRecordPage)
            } else if (url == 'UserServicePage') {
                this.navCtrl.push(UserServicePage)
            }
        } else {
            this.pubCom.doubleAlert()
        }
    }

    cashPage() {
        this.navCtrl.push(UserCashPage)
    }
    rechargePage() {
        this.navCtrl.push(UserRechargePage)
    }

    // 查看绑定的银行卡信息
    CheckBank() {
        let timeStamp = (new Date()).getTime();
        if (this.uid) {
            this.Api.isBindBank({
                times: timeStamp,
                token: Md5.hashStr('HalfPriceMall' + timeStamp),
                userid: this.uid
            }).then(res => {
                this.resData = res;
                if (this.resData.code == 1) {
                    if (this.resData.data.isblank == 0) {
                        // 没有绑定银行卡
                        this.navCtrl.push(UserBindBankPage)
                    } else {
                        this.pubCom.showAlert('提示', '您已经绑定过银行卡。如需更改，请联系客服！', '确定', console.log('确定'))
                    }
                } else {
                    this.pubCom.toast(this.resData.msg, 'middle');
                }
            })
        }
    }

}
